package com.jeesite.modules.common.web;

import com.jeesite.modules.common.service.OldDataService;
import com.jeesite.modules.file.entity.FileEntity;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.service.FileEntityService;
import com.jeesite.modules.task.deploy.dao.DcSupTaskInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 老系统数据迁移控制器
 */
@Controller
public class OldDataController {
    @Autowired
    private OldDataService oldDataService;
    @Autowired
    private DcSupTaskInfoDao dcSupTaskInfoDao;


    /**
     * 同步任务 task_info表
     * @return
     */
    @RequestMapping(value = "findlistTask")
    @ResponseBody
    public String findlistTask(){
        String code=oldDataService.findlistTask();
        //修改任务发起单位
        oldDataService.updateInfoCreateby();
        //修改info反馈要求
        oldDataService.updateInfoFeedback();
        return code;
    }


    /**
     * 同步任务 task_action表
     * @return
     */
    @RequestMapping(value = "findlistTaskAction")
    @ResponseBody
    public String findlistTaskAction(){
        String code=oldDataService.findlistTaskAction();
        return code;
    }

    /**
     * 同步任务 feedback表
     * @return
     */
    @RequestMapping(value = "findlistTaskFeedback")
    @ResponseBody
    public String findlistTaskFeedback(){
        String code=oldDataService.findlistTaskFeedback();
        //修改反馈人
        oldDataService.updateFeedbackCreateby();
        return code;
    }


    /**
     * 同步任务审核记录
     */
    @RequestMapping(value = "findTaskAudit")
    @ResponseBody
    public String findTaskAudit(){
        oldDataService.findTaskAudit();
        return "ok";
    }

    public static int count=0;
    public static int errorCount=0;
    /**
     * 遍历文件,根据不同表筛选文件
     * @return
     */
    @RequestMapping(value = "screenFile")
    @ResponseBody
    public String screenFile(){
        List<String> list=changeFileNames();
        System.out.println("导入文件成功"+count);
        System.out.println("导入文件失败"+errorCount);
        for (String s:list) {
            System.out.println(s);
        }
        return "ok";
    }

    public List<String> changeFileNames(){
        count=0;
        errorCount=0;
        //task文件
//        List<Map<Object,Object>> actionList=oldDataService.findTaskFile();
        //feedback文件
//        List<Map<Object,Object>> actionList=oldDataService.findFeedbackFile(ss);
        // 已完成task文件
//        List<Map<Object,Object>> actionList=oldDataService.findCompleteTaskFile();
        // 已完成feedback文件
        List<Map<Object,Object>> actionList=oldDataService.findCompleteFeedbackFile();
        //2022年之前完成的任务task文件
//        List<Map<Object,Object>> actionList=oldDataService.findBeforeTaskFile();
        //2022年之前完成的任务task文件
//        List<Map<Object,Object>> actionList=oldDataService.findBeforeFeedbackFile();
        List<String> list=new ArrayList<>();
        int i=0;
        for (Map<Object,Object> map:actionList) {
            String fileName=(String) map.get("filenamemd5");
            if("file".equals(fileName)){
                continue;
            }
             File file=new File("D:/desktop/whdcFile/whdc/"+fileName);
             fileName = "D:/desktop/whdcFile/feedBackFile2022/file/"+fileName;
            //修改文件名
            boolean b = file.renameTo(new File(fileName));
            if(b){
                count++;
            }else {
                list.add(fileName);
                errorCount++;
            }
        }
        return list;
    }
    /**
     * 遍历文件,从fastdfs文件夹筛选文件
     * @return
     */
    @RequestMapping(value = "fastdfsFile")
    @ResponseBody
    public void changeFileName(String path){
        int i=0;
        try {
            File file=new File(path);
            //判断文件夹是否存在
            if(file.exists()){
                File[] files = file.listFiles();
                //判断文件夹是否为空
                if (null == files || files.length == 0) {
                    System.out.println("文件夹是空的!");
                    return;
                } else {
                    //遍历每个文件
                    for (File file2 : files) {
                        //判断是否是文件夹，进行递归
                        if (file2.isDirectory()) {
                            changeFileName(file2.getAbsolutePath());
                        } else {
                            //获取文件路径
                            String filePath = file2.getAbsolutePath();
                            String fileName=file2.getName();
                            if("-m".equals(fileName.substring(fileName.length()-2,fileName.length()))){
                                continue;
                            }
                            //拼接文件新路径名称
                            fileName = "D:/desktop/actionFile/"+fileName;
                            //根据文件路径名称获取文件
                            File oriFile = new File(filePath);
                            //修改文件名
                            boolean b = oriFile.renameTo(new File(fileName));
                            if(b){
                                count++;
                            }else {
                                errorCount++;
                            }
                        }

                    }
                }
            }else{
                System.out.println("该路径不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改2022未完成task_info文件名称和文件绑定类型
     * @return
     */
    @RequestMapping(value = "updateTaskInfo")
    @ResponseBody
    public String updateTaskInfo(){
        List<Map<Object,Object>> actionList=oldDataService.findTaskFile();
        for (Map<Object,Object> map:actionList) {
            String fileName=(String) map.get("filenamemd5");
            if("file".equals(fileName)){
                continue;
            }
            FileUpload fileUpload=new FileUpload();
            fileUpload.setFileName(fileName);
            fileUpload.setBizKey(String.valueOf(map.get("ptaskid")));
            fileUpload.setBizType((String) map.get("filename"));
            //dcSupTaskFeedback_file
            //dcSupTaskInfo_file
            dcSupTaskInfoDao.updateUploudFile(fileUpload);
        }
        return "ok";
    }

    /**
     * 修改2022未完成feedback文件名称和文件绑定类型
     * @return
     */
    @RequestMapping(value = "updateAction")
    @ResponseBody
    public String updateAction(){
        List<Map<Object,Object>> actionList=oldDataService.findFeedbackFile("");
        for (Map<Object,Object> map:actionList) {
            String fileName=(String) map.get("filenamemd5");
            if("file".equals(fileName)){
                continue;
            }
            FileUpload fileUpload=new FileUpload();
            fileUpload.setFileName(fileName);
            fileUpload.setBizKey(String.valueOf(map.get("businessid")));
            fileUpload.setBizType((String) map.get("filename"));
            //dcSupTaskFeedback_file
            //dcSupTaskInfo_file
            dcSupTaskInfoDao.updateUploudFile(fileUpload);
        }
        return "ok";
    }

    /**
     * 修改已完成task_info文件名称和文件绑定类型
     * @return
     */
    @RequestMapping(value = "updateCompleteTaskInfo")
    @ResponseBody
    public String updateCompleteTaskInfo(){
        List<Map<Object,Object>> actionList=oldDataService.findCompleteTaskFile();
        for (Map<Object,Object> map:actionList) {
            String fileName=(String) map.get("filenamemd5");
            if("file".equals(fileName)){
                continue;
            }
            FileUpload fileUpload=new FileUpload();
            fileUpload.setFileName(fileName);
            fileUpload.setBizKey(String.valueOf(map.get("ptaskid")));
            fileUpload.setBizType((String) map.get("filename"));
            //dcSupTaskFeedback_file
            //dcSupTaskInfo_file
            dcSupTaskInfoDao.updateUploudFile(fileUpload);
        }
        return "ok";
    }

    /**
     * 修改已完成feedback文件名称和文件绑定类型
     * @return
     */
    @RequestMapping(value = "updateCompleteAction")
    @ResponseBody
    public String updateCompleteAction(){
        List<Map<Object,Object>> actionList=oldDataService.findCompleteFeedbackFile();
        for (Map<Object,Object> map:actionList) {
            String fileName=(String) map.get("filenamemd5");
            if("file".equals(fileName)){
                continue;
            }
            FileUpload fileUpload=new FileUpload();
            fileUpload.setFileName(fileName);
            fileUpload.setBizKey(String.valueOf(map.get("businessid")));
            fileUpload.setBizType((String) map.get("filename"));
            //dcSupTaskFeedback_file
            //dcSupTaskInfo_file
            dcSupTaskInfoDao.updateUploudFile(fileUpload);
        }
        return "ok";
    }

    //------------------------------------------分割线-----------------------------------------------------------------------------

    /**
     * 同步任务 2022年之前的 task_info表
     * @return
     */
    @RequestMapping(value = "findBeforelistTask")
    @ResponseBody
    public String findBeforelistTask(){
        String code=oldDataService.findBeforelistTask();
        //修改任务发起单位
        oldDataService.updateInfoCreateby();
        //修改info反馈要求
        oldDataService.updateInfoFeedback();
        return code;
    }

    /**
     * 同步任务 2022年之前 task_action表
     * @return
     */
    @RequestMapping(value = "findBeforelistTaskAction")
    @ResponseBody
    public String findBeforelistTaskAction(){
        String code=oldDataService.findBeforelistTaskAction();
        return code;
    }

    /**
     * 同步任务2022年之前 feedback表
     * @return
     */
    @RequestMapping(value = "findBeforelistTaskFeedback")
    @ResponseBody
    public String findBeforelistTaskFeedback(){
        String code=oldDataService.findBeforelistTaskFeedback();
        //修改反馈人
        oldDataService.updateFeedbackCreateby();
        return code;
    }

    /**
     * 同步任务2022年之前 审核记录
     */
    @RequestMapping(value = "findBeforeTaskAudit")
    @ResponseBody
    public String findBeforeTaskAudit(){
        oldDataService.findBeforeTaskAudit();
        return "ok";
    }

    /**
     * 修改2022之前task_info文件名称和文件绑定类型
     * @return
     */
    @RequestMapping(value = "updateBeforeTaskInfo")
    @ResponseBody
    public String updateBeforeTaskInfo(){
        List<Map<Object,Object>> actionList=oldDataService.findBeforeTaskFile();
        for (Map<Object,Object> map:actionList) {
            String fileName=(String) map.get("filenamemd5");
            if("file".equals(fileName)){
                continue;
            }
            FileUpload fileUpload=new FileUpload();
            fileUpload.setFileName(fileName);
            fileUpload.setBizKey(String.valueOf(map.get("ptaskid")));
            fileUpload.setBizType((String) map.get("filename"));
            //dcSupTaskFeedback_file
            //dcSupTaskInfo_file
            dcSupTaskInfoDao.updateUploudFile(fileUpload);
        }
        return "ok";
    }

    /**
     * 修改2022之前feedback文件名称和文件绑定类型
     * @return
     */
    @RequestMapping(value = "updateBeforeAction")
    @ResponseBody
    public String updateBeforeAction(){
        List<Map<Object,Object>> actionList=oldDataService.findBeforeFeedbackFile();
        for (Map<Object,Object> map:actionList) {
            String fileName=(String) map.get("filenamemd5");
            if("file".equals(fileName)){
                continue;
            }
            FileUpload fileUpload=new FileUpload();
            fileUpload.setFileName(fileName);
            fileUpload.setBizKey(String.valueOf(map.get("businessid")));
            fileUpload.setBizType((String) map.get("filename"));
            //dcSupTaskFeedback_file
            //dcSupTaskInfo_file
            dcSupTaskInfoDao.updateUploudFile(fileUpload);
        }
        return "ok";
    }

}
